package com.hmall.controller.back;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.EnterpriseInfoMapper;
import com.hmall.mapper.UnsearchKeywordMapper;
import com.hmall.model.EnterpriseInfo;
import com.hmall.model.UnsearchKeyword;
import com.hmall.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/manage/exam")
@Api(value = "企业信息管理操作API", description = "企业信息管理类API说明")
public class EnterpriseController {
    @Autowired
    private final EnterpriseInfoMapper enterpriseInfoMapper;

    @Autowired
    private final UnsearchKeywordMapper unsearchKeywordMapper;

    public EnterpriseController(EnterpriseInfoMapper enterpriseInfoMapper, UnsearchKeywordMapper unsearchKeywordMapper) {
        this.enterpriseInfoMapper = enterpriseInfoMapper;
        this.unsearchKeywordMapper = unsearchKeywordMapper;
    }

    @GetMapping("GetEnterpriseList")
    @ApiOperation(value = "获取公司信息列表")
    public ServerResponse GetCustomerList(String keyword, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = enterpriseInfoMapper.selectCountByKeyWord(keyword);
        List<EnterpriseInfo> list = enterpriseInfoMapper.selectByKeyWord(keyword, pageStart, pageSize);
        if (list == null || list.size() == 0) {
            UnsearchKeyword unsearchKeyword = new UnsearchKeyword();
            Integer count = unsearchKeywordMapper.selectByKeword(keyword);
            if (count == 0) {
                unsearchKeyword.setKeyword(keyword);
                unsearchKeywordMapper.insert(unsearchKeyword);
            }
            return ServerResponse.createByErrorMessage("没有搜索到指定内容");
        }
        PageVo<EnterpriseInfo> pageVo = new PageVo<>(pageNum, pageSize, total, list);


        return ServerResponse.createBySuccess(pageVo);
    }

    @GetMapping("GetEnterprise")
    @ApiOperation(value = "获得公司信息")
    public ServerResponse GetEnterprise(String companyName) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectByCompanyName(companyName);
        return ServerResponse.createBySuccess(enterpriseInfo);
    }

    @PostMapping("AddEnterprise")
    @ApiOperation(value = "添加公司信息")
    public ServerResponse AddEnterprise(@RequestBody EnterpriseInfo enterpriseInfo) {
        EnterpriseInfo enterpriseInfo2 = enterpriseInfoMapper.selectByCompanyName(enterpriseInfo.getCompanyName());
        if (enterpriseInfo2 != null) {
            return ServerResponse.createByErrorMessage("该公司已存在");
        }
        enterpriseInfo.setUpdateTime(new Date());
        enterpriseInfoMapper.insert(enterpriseInfo);
        return ServerResponse.createBySuccess("添加成功");
    }

    @PostMapping("UpdateEnterprise")
    @ApiOperation(value = "更新公司信息")
    public ServerResponse UpdateEnterprise(@RequestBody EnterpriseInfo enterpriseInfo) {
        EnterpriseInfo enterpriseInfo2 = enterpriseInfoMapper.selectByCompanyName(enterpriseInfo.getCompanyName());
        if (enterpriseInfo2 == null) {
            return ServerResponse.createByErrorMessage("该公司不存在");
        }
        enterpriseInfo.setUpdateTime(new Date());
        enterpriseInfoMapper.updateByPrimaryKey(enterpriseInfo);
        return ServerResponse.createBySuccess("更新成功");
    }

    @DeleteMapping("DeleteEnterprise")
    @ApiOperation(value = "删除公司信息")
    public ServerResponse DeleteEnterprise(String companyName) {
        enterpriseInfoMapper.deleteByPrimaryKey(companyName);
        return ServerResponse.createBySuccess("删除成功");
    }
}
